﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace ZhaoPin.Feature
{
    public partial class WorkStoryPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            Guid guid = (Guid)Membership.GetUser().ProviderUserKey;
            Session["UserId"] = guid;
            if (!Page.IsPostBack)
            {
            }
        }



        protected void gvWorkStory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = -1;
            switch (e.CommandName)
            {
                case "Edit":
                    index = GetEditIndex(sender as GridView, int.Parse(e.CommandArgument.ToString()));
                    FormView1.PageIndex = index;
                    FormView1.ChangeMode(FormViewMode.Edit);
                    FormView1.Visible = true;
                    break;
                case "Insert":
                    FormView1.ChangeMode(FormViewMode.Insert);
                    FormView1.Visible = true;
                    break;
                default:
                    break;
            }
        }
        protected int GetEditIndex(GridView view, int rowIndex)
        {
            int r = -1;
            if (view.AllowPaging)
                r = view.PageSize * view.PageIndex + rowIndex;
            else
                r = rowIndex;
            return r;
        }

        protected void FormView1_PreRender(object sender, EventArgs e)
        {
            FormView fv = sender as FormView;
            TextBox tb = fv.FindControl("tbStartDate") as TextBox;
            tb.Attributes.Add("readonly", "true");
            tb = fv.FindControl("tbEndDate") as TextBox;
            tb.Attributes.Add("readonly", "true");
        }



        protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
        {
            FormView fv = sender as FormView;
            if (e.CancelingEdit)
            {
                fv.Visible = false;
            }
           
        }

        

        protected void gvWorkStory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton b = e.Row.Cells[0].FindControl("btnEdit") as LinkButton;
                b.CommandArgument = e.Row.RowIndex.ToString();
            }
        }

        protected void gvWorkStory_RowEditing(object sender, GridViewEditEventArgs e)
        {
            e.Cancel = true;
        }

        protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            FormView1.Visible = false;
            gvWorkStory.DataBind();
        }

        protected void gvWorkStory_EntityDataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
        {
            WorkStory ws = e.Entity as WorkStory;
            Guid guid = (Guid)Membership.GetUser().ProviderUserKey;
            ws.PersonId = guid;
            e.Context.SaveChanges();
            gvWorkStory.DataBind();
        }
    }
}